package domain.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;

public class Filler {

	private static final String NEW_LINE = System.getProperty("line.separator");

	public static String getJSFunction(String record) {

		Scanner sc = new Scanner(record);

		StringBuilder out = new StringBuilder(record.length());

		// appendLine(out, "function preenche_campos() {");
		appendAssertionLine(out, "tag_000_subfield_00", "'"
				+ sc.nextLine().trim().replaceAll("\\Q'\\E", "\\'") + "'");

		while (sc.hasNextLine()) {
			String line = sc.nextLine().trim();

			if (line.equals(""))
				continue;

			String tag = line.split("\\s+")[0];

			StringBuilder builder = new StringBuilder();
			for (int i = tag.length(); i < line.length(); i++) {
				builder.append(line.charAt(i));
			}
			String rest = builder.toString();

			int indexDollar = rest.trim().replaceAll(" ", "").indexOf('$');
			char c1 = rest.trim().charAt(0);
			char c2 = rest.trim().charAt(1);

			if (indexDollar == -1 || indexDollar > 2) {
				appendAssertionLine(out, "tag_" + tag + "_subfield_00", "'"
						+ rest.trim().replaceAll("\\Q'\\E", "\\'") + "'");
				continue;
			}

			if (Character.isDigit(c1)) {
				appendAssertionLine(out, "tag_" + tag + "_indicator1", "'"
						+ String.valueOf(c1) + "'");
				appendAssertionLine(out, "tag_" + tag + "_indicator2", "'"
						+ String.valueOf(c2) + "'");
			}

			String[] tokens = rest.split("\\Q$\\E");

			for (int i = 1; i < tokens.length; i++) {
				String token = tokens[i].trim();

				char sf = token.charAt(0);

				appendAssertionLine(out, "tag_" + tag + "_subfield_" + sf, "'"
						+ token.substring(1).trim()
								.replaceAll("\\Q'\\E", "\\'") + "'");
			}
		}

		// appendLine(out, "}");

		return out.toString();
	}

	private static void appendAssertionLine(StringBuilder out,
			String parameter, String value) {

		if (parameter.contains("indicator")) {
			appendTabbedLine(out,
					("if (window.parent.document.getElementsByName('" + parameter + "') != null)"));
			appendTabbedLine(out, "window.parent.document.getElementsByName('"
					+ parameter + "')[0].value = " + value + ";");
		} else {
			appendTabbedLine(out,
					("if (window.parent.document.getElementById('" + parameter + "') != null)"));
			appendTabbedLine(out, "window.parent.document.getElementById('"
					+ parameter + "').value = " + value + ";");
		}
	}

	private static void appendTabbedLine(StringBuilder out, String string) {
		out.append("\t");
		appendLine(out, string);
	}

	private static void appendLine(StringBuilder out, String string) {
		out.append(string + NEW_LINE);
	}

	public static void main(String[] args) throws Exception {

		BufferedReader br = new BufferedReader(new FileReader(
				new File("teste1")));

		int c;
		StringBuilder record = new StringBuilder();
		while ((c = br.read()) != -1) {
			record.append((char) c);
		}

		System.out.println(record);

		System.out.println(getJSFunction(record.toString()));
	}
}
